package com.sm.li.common.perm;
import com.sm.li.common.exception.ApiException;
import com.sm.li.common.http.AxiosStatus;
import com.sm.li.components.TokenService;
import com.sm.li.entity.Menu;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

@Aspect
@Component
public class permAop {

    @Autowired
    private TokenService tokenService;

    @Before("pointCut()")
    public void checkBtnPrem(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        HasPerm annotation = method.getAnnotation(HasPerm.class);
        if (annotation!=null){
            String s = annotation.permSign();

            List<Menu> menus = tokenService.getLoginAdminCache().getMenus();
            boolean b = menus.stream().anyMatch(menu -> s.equalsIgnoreCase(menu.getPermSign()));
            if (!b){
                throw  new ApiException(AxiosStatus.NO_PERMISSION);
            }
            //和数据库中的员工的权限对比
            //如果员工权限标识有这个值证明有权限

            //没有这个值证明没有权限 抛出异常

        }
    }

    @Pointcut("execution(* com.sm.li.controller..*.*(..))")
    public void pointCut(){

    }
}
